import fs from "fs";
import path from "path";
import { decryptUTF8 } from "siphan";
import { safeWrite } from "./utils";

function solve(cipher: string, cipherPath: string, outputName: string) {
	fs.readFile(path.resolve(process.cwd(), cipherPath), (err, data) => {
		if (err !== null) {
			throw err;
		}
		safeWrite(
			path.resolve(process.cwd(), outputName),
			decryptUTF8(data.toString(), cipher, e => console.log(e))!
		);
	});
}

export default function decryptMain(
	keyPath: string,
	cipherPath: string,
	outputName: string
) {
	fs.readFile(path.resolve(process.cwd(), keyPath), (err, data) => {
		if (err !== null) {
			throw err;
		}
		solve(data.toString(), cipherPath, outputName);
	});
}
